package com.kang.controller;

import com.kang.model.Student;
import com.kang.service.StudentService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/1/3.
 */
@Controller
public class LoginController {

    @Autowired
    private StudentService service;

    @RequestMapping("/login")
    public String login() {
        return "/admin/login";
    }

    @RequestMapping("/loginApprove")
    @ResponseBody
    public Map<String,String> loginApprove(@RequestParam("username") String name,@RequestParam("password") String pwd,Model model) {

        Map<String,String> jsonMap = new HashMap<String,String>();

        if(StringUtils.isNotEmpty(name))
        {
            //验证name是否存在
            List<Student> stdList = service.findStdByName(name);
            if(CollectionUtils.isNotEmpty(stdList) && stdList.size()>0)
            {
                if(StringUtils.isNotEmpty(pwd))
                {
                    Student std = service.findStdByNameAndPwd(name,pwd);
                    if(null != std)
                    {
                        jsonMap.put("result","success");
                    }else
                    {
                        jsonMap.put("result","pwdFalse");
                    }
                }
            }else
            {
                jsonMap.put("result","nameFalse");
            }
        }
        return jsonMap;
    }
}
